package com.sks.web.action.house;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.QueryResult;
import com.sks.bean.pojo.Message;
import com.sks.service.house.MessageService;
import com.sks.utils.SiteUrl;
import com.sks.web.action.BasicAction;
import com.sks.web.action.privilege.Permission;
import com.sks.web.formbean.house.MessageForm;

@Controller("/cms/message")
public class MessageAction extends BasicAction {
	@Resource(name = "messageServiceBean") private MessageService service;
	
	@Permission(module = "message", privilege = "view")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageForm formbean = (MessageForm) form;
		StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		PageView<Message> pageView = new PageView<Message>(12, formbean.getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("createTime", "asc");
		QueryResult<Message> qr = service.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby);
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);
		this.setReadOnly(request, "list");
		return mapping.findForward(LIST);
	}
	
	@Permission(module = "message", privilege = "view")
	public ActionForward info(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageForm formbean = (MessageForm) form;
		request.setAttribute("message", service.find(formbean.getMessageId()));
		return mapping.findForward(EDIT);
	}
	
	@Permission(module = "message", privilege = "update")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageForm formbean = (MessageForm) form;
		service.delete(formbean.getMessageId());
		request.setAttribute(MESSAGE, getMessage("display.delete") + getMessage("info.message.name") + getMessage("display.success"));
		request.setAttribute("urladdress", SiteUrl
				.readUrl("com.sks.web.action.message"));
		return mapping.findForward(MESSAGE);
	}
	
	@Permission(module = "message", privilege = "update")
	public ActionForward mark(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageForm formbean = (MessageForm) form;
		String result = null;
		try {
			Message msg = service.find(formbean.getMessageId());
			msg.setRead(true);
			service.update(msg);
			result = "{\"messageId\":" + formbean.getMessageId() + ",\"success\":true}";
		} catch (Exception e) {
			result = "{\"messageId\":" + formbean.getMessageId() + ",\"success\":false}";
		}
		response.getWriter().print(result);
		return null;
	}
}
